﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.SMS.Data;
using System.Web.Routing;
namespace Wicresoft.SMS.Web.Controllers
{
    public class SendController : CustomBaseController
    {
        //
        // GET: /Send/
        [CustomAuthorise(Flag = AuthoriseFlag.page_addressbook)]
        public ActionResult Index(AddressSendModle Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IAddressWebDataAccess dataAccess = new sqlWebAddressDataAccess(configProvider.ConnectionString);
            ITemplateService TdataAccess = new TemplateDataAccess(configProvider);
            //得到当前用户
            IUserService us = new UserService(configProvider);
            int userid = us.GetUserItemByCIL(User.Identity.Name).UserID;
            Model.Infos = dataAccess.GetNumberWithGroupByUsers(userid);
            Model.Tinfos = TdataAccess.GetTemplatesByUserIDS(userid);
            if (Model.content == null)
            {
                Model.content = "";
            }
            return View(Model);
        }

        //转发
         [CustomAuthorise(Flag = AuthoriseFlag.page_sms_template)]
        public ActionResult forwardmessage(int Id)
        {
             WebConfigProvider configProvider = new WebConfigProvider();
             int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
             ISendWebDataAccess dataAccess = new sqlWebSendDataAccess(configProvider.ConnectionString);
             string c = dataAccess.forwardmessage(Id);
             AddressSendModle Model = new AddressSendModle();
             Model.content = c;
             ITemplateService TdataAccess = new TemplateDataAccess(configProvider);
             IAddressWebDataAccess dataAccessAdd = new sqlWebAddressDataAccess(configProvider.ConnectionString);
             //得到当前用户
             IUserService us = new UserService(configProvider);
             int userid = us.GetUserItemByCIL(User.Identity.Name).UserID;
             Model.Infos = dataAccessAdd.GetNumberByUsers(userid);
             Model.Tinfos = TdataAccess.GetTemplatesByUserIDS(userid);
             if (Model.content == null)
             {
                 Model.content = "";
             }
             return View("Index",Model);
        }

        //模板
        [CustomAuthorise(Flag = AuthoriseFlag.page_sms_template)]
        public ActionResult TemplateByUser()
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            ITemplateService dataAccess = new TemplateDataAccess(configProvider);
            int max = Convert.ToInt32(configProvider.MaxshowInHistory);
            //得到当前用户
            IUserService us = new UserService(configProvider);
            int userid = us.GetUserItemByCIL(User.Identity.Name).UserID;
            TemplateModel Amodel = new TemplateModel();
            Amodel.list = dataAccess.GetTemplatesByUserIDS(userid);
            return View(Amodel);
        }

        //发送短信
         [CustomAuthorise(Flag = AuthoriseFlag.page_sms_send)]
        public ActionResult Send(string addresslist, string addresslists)
        {            
             if (addresslist.Trim() == "")
            {
                //App_GlobalResources.Resources.Controller_Send_Need_User
                TempData["msg"] =  App_GlobalResources.Resources.Controller_Send_Need_User;
                return Redirect("index");
            }
            if (addresslists.Trim() == "")
            {
                TempData["msg"] = App_GlobalResources.Resources.Controller_Send_Need_Content;
                return Redirect("index");
            }
            else
            {
                WebConfigProvider configProvider = new WebConfigProvider();
                ISendWebDataAccess dataAccess = new sqlWebSendDataAccess(configProvider.ConnectionString);
                string numbers = addresslist.Substring(addresslist.Length - 1, 1);
                if (numbers != ";")
                {
                    addresslist += ";";
                }
                //得到当前用户
                IUserService us = new UserService(configProvider);
                string username = us.GetUserItemByCIL(User.Identity.Name).CIL;
                int result = dataAccess.SendSms(addresslist, addresslists, username);
                //根据返回值判断是否短信成功放入数据库
                if (result == 1)
                {
                    TempData["msg"] = App_GlobalResources.Resources.Controller_Send_WrongUser;
                }
                else if (result == 2)
                {
                    TempData["msg"] = App_GlobalResources.Resources.Controller_Send_WrongNum;
                }
                //else if (result == 3)
                //{
                //    TempData["msg"] = "本月信息已发完";
                //}
                else if (result == 4)
                {
                    TempData["msg"] = App_GlobalResources.Resources.Controller_Send_UnknownWrong;
                }
                else if (result == 0)
                {
                    TempData["msg"] = App_GlobalResources.Resources.Controller_Send_Success;
                }
                else if(result == -1)
                {
                    TempData["msg"] = App_GlobalResources.Resources.Controller_Send_UnknownWrong;
                }
                return Redirect("index");
            }
        }

    }
}
